package com.xiangxiangjd.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xiangxiangjd.bean.TrUser;
import com.xiangxiangjd.controller.helper.$;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import com.xiangxiangjd.bean.result.ResultPage;
import org.springframework.stereotype.Controller;
import com.xiangxiangjd.service.UnitService;
import com.xiangxiangjd.bean.Unit;
import com.xiangxiangjd.controller.model.ApiResultModel;

import java.util.ArrayList;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 前端控制器
 *
 * @author 肖守凯
 * @since 2021-07-15
 */
@Controller
@RequestMapping("/api/unit")
@Api(tags = "单位")
public class UnitController extends BaseController {

  /**
   * 分页查询Unit
   *
   * @param page 页码
   * @param limit 查几条
   * @param unit 前端传进的参数
   * @return 返回统一分页信息
   */
  @PostMapping(value = "admin/findUnitByPage")
  @ResponseBody
  @PreAuthorize("hasRole('ROLE_USER')")
  @ApiOperation(value = "分页查询", notes = "分页查询")
  public ResultPage findUnitByPage(Integer page, Integer limit, Unit unit) {
    return unitService.findUnitByPage(page, limit, unit);
  }

  /**
   * 添加Unit
   *
   * @param unit 前端传进的参数
   * @return 返回统一信息
   */
  @PostMapping(value = "admin/addUnit")
  @ResponseBody
  @PreAuthorize("hasRole('ROLE_ADMIN')")
  @ApiOperation(value = "添加单位", notes = "添加单位")
  public ApiResultModel saveUnit(Unit unit) {

    if (unit.getUnitName() == null) {
      return $.error("角色名称不可为空");
    }
    if (unit.getParentId() == null) {
      return $.error("父级id 不可为空");
    }

    if (unit.getParentId().equals("0")) {
      List<Unit> list =
          unitService.list(
              new LambdaQueryWrapper<Unit>().eq(Unit::getUnitName, unit.getUnitName()));
      if (list.size() > 0) {
        return $.error("该大类已存在");
      }
    }

    System.out.println(unit.toString());
    return unitService.addUnit(unit);
  }

  /**
   * 编辑Unit
   *
   * @param unit 前端传进的参数
   * @return 返回统一信息
   */
  @PostMapping(value = "admin/editUnit")
  @ResponseBody
  @PreAuthorize("hasRole('ROLE_ADMIN')")
  @ApiOperation(value = "编辑单位", notes = "编辑单位")
  public ApiResultModel editUnit(Unit unit) {

    List<Unit> getUnitName = unitService.list(new LambdaQueryWrapper<Unit>().eq(Unit::getUnitName, unit.getUnitName()));

      if(getUnitName.size()>0){
        return $.error("该单位名称已存在！");
      }

    if (unit.getUnitName() == null) {
      return $.error("角色名称不可为空");
    }
    if (unit.getParentId() == null) {
      return $.error("父级id 不可为空");
    }
    if (unit.getParentId().equals("0")) {
      List<Unit> list =
          unitService.list(
              new LambdaQueryWrapper<Unit>().eq(Unit::getUnitName, unit.getUnitName()));
      if (list.size() > 0) {
        return $.error("该大类已存在");
      }
    }

    return unitService.editUnit(unit);
  }

  /**
   * 删除Unit
   *
   * @param ids 前端传进的ID
   * @return 返回统一信息
   */
  @PostMapping(value = "admin/delUnit")
  @ResponseBody
  @PreAuthorize("hasRole('ROLE_ADMIN')")
  @ApiOperation(value = "删除单位", notes = "删除单位")
  public ApiResultModel delUnit(@RequestParam("datas") ArrayList ids) {

    for (int i = 0; i < ids.size(); i++) {

      // 查询有没有 正在用这个单位的用户
      List<TrUser> list =
          trUserService.list(new LambdaQueryWrapper<TrUser>().eq(TrUser::getUnitId, ids.get(i)));

      if (list.size() > 0) {
        return $.error("该单位已有用户 在使用不可删除");
      }
      // 查询有没有子集单位
      List<Unit> list2 =
          unitService.list(new LambdaQueryWrapper<Unit>().eq(Unit::getParentId, ids.get(i)));
      if (list2.size() > 0) {
        return $.error("请先删除子集 单位");
      }
    }

    return unitService.delUnits(ids);
  }

  /**
   * 获取角色树
   *
   * @param
   * @return
   */
  @GetMapping(value = "/getTreeUnit")
  @ResponseBody
  @ApiOperation(value = "获取单位树行结构", notes = "获取单位树行结构")
  @PreAuthorize("hasRole('ROLE_USER')")
  public ApiResultModel getTreeUnit() {

    List<Unit> data = unitService.getTreeUnit();

    return $.success(data);
  }

  /** 获取该单位的所有的上级 */
  @GetMapping(value = "/getFatherUnitAll")
  @ResponseBody
  @ApiOperation(value = "获取该单位的所有的上级", notes = "获取该单位的所有的上级")
  @PreAuthorize("hasRole('ROLE_USER')")
  public ApiResultModel getFatherUnitAll(@RequestParam(value = "unitId") String unitId) {

    List<Unit> unitServiceFatherAll = unitService.getFatherAll(unitId);

    return $.success(unitServiceFatherAll);
  }

}
